package com.hq.ims.data.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hq.authority.utils.AppUtils;
import com.hq.ims.data.entity.Company;
import com.hq.ims.data.entity.User;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.utils.WebExpandUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.inf.user.ITokenUserInfo;
import com.hq.web.utils.ApplicationContextHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * jiang bai ping
 *
 * @Email 526666031@qq.com
 */
@Slf4j
@Component
public class ToBMetaObjectHandler implements MetaObjectHandler {

    private final String CREATE_DATE_TIME = "createDateTime";
    private final String CREATE_USER_ID = "createUserId";
    private final String CREATE_USER_NAME = "createUserName";

    private final String UPDATE_DATE_TIME = "updateDateTime";
    private final String UPDATE_USER_ID = "updateUserId";
    private final String UPDATE_USER_NAME = "updateUserName";

    private final String VERSION = "version";

    private final String ORG_ID = "orgId";
    private final String ORG_NAME = "orgName";

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        this.fillStrategy(metaObject, CREATE_DATE_TIME, now);
        this.fillStrategy(metaObject, UPDATE_DATE_TIME, now);
        ITokenUserInfo currentUser = Optional.ofNullable(AppUtils.getCurrentUserNoException()).orElse(new User());
        this.fillStrategy(metaObject, CREATE_USER_ID, StringUtils.ifEmptyDefault(currentUser.getId()));
        this.fillStrategy(metaObject, CREATE_USER_NAME, StringUtils.ifEmptyDefault(currentUser.getRealName()));
        this.fillStrategy(metaObject, UPDATE_USER_ID, StringUtils.ifEmptyDefault(currentUser.getId()));
        this.fillStrategy(metaObject, UPDATE_USER_NAME, StringUtils.ifEmptyDefault(currentUser.getRealName()));
        this.fillStrategy(metaObject, VERSION, 1);

        //获取当前公司
        String curCompanyId = StringUtils.EMPTY;
        //若是接口请求从header获取id
        if (RequestContextHolder.getRequestAttributes() != null) {
            curCompanyId = WebExpandUtils.getHeadCompanyId();
        }

        if (StringUtils.isNotEmpty(curCompanyId)) {
            this.strictInsertFill(metaObject, ORG_ID, String.class, curCompanyId);
            BaseCompanyService baseCompanyService = ApplicationContextHelper.getBean(BaseCompanyService.class);
            Company company = baseCompanyService.getById(curCompanyId);
            if (company != null) {
                this.strictInsertFill(metaObject, ORG_NAME, String.class, company.getCompanyName());
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.fillStrategy(metaObject, UPDATE_DATE_TIME, LocalDateTime.now());
        ITokenUserInfo currentUser = AppUtils.getCurrentUserNoException();
        if (currentUser != null) {
            this.fillStrategy(metaObject, UPDATE_USER_ID, currentUser.getId());
            this.fillStrategy(metaObject, UPDATE_USER_NAME, currentUser.getRealName());
        }
    }
}
